<template>
    <el-row class="user-info-form" type="flex" justify="center">
        <el-col>
          <el-form label-width="110px" :model="editForm" :rules="editFormRules" ref="editForm" @submit.native.prevent>
            <el-form-item label="版本名称：" prop='versionId'>
              <el-select filterable placeholder="请选择版本名称"  v-model="editForm.versionId">
                <el-option
                  v-for="item in versionOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label-width="0" class="verifyidentity-button">
              <el-button round @click="closeDialog()">取消</el-button>
              <el-button type="primary" round @click="saveEdit('editForm')">提交</el-button>
            </el-form-item>
          </el-form>
        </el-col>
    </el-row>
</template>
<script>

import { getVersionList,batchUpdateUsageVersion} from '@/api/dcx'

export default {
    mame: 'PlatTransDeviceForm',
    props: {
        editInfo: {
            type: Object,
            required: false
        },
        closeDialog: {
            type: Function,
            required: true
        },
    },
    data() {
        return {
          versionOptions: [],
          editForm: {
              id: '',
              versionId: '',
          },
          editFormRules: {
              versionId: [
                  { required: true, message: '请选择版本', trigger: 'blur' }
              ],
          },
        }
    },
    created() {
        getVersionList({}).then((data)=>{
          this.versionOptions = data.data
        })
        let infoObj = this.editInfo
        if(infoObj.id) {//编辑
          this.editForm.id = infoObj.id
        }
    },
    methods: {
        saveEdit(formName) {
            // 阻止事件冒泡
            event.stopPropagation()
            let that = this
            that.$refs[formName].validate((valid) => {
                if (valid) {
                    let parames = that.editForm
                    batchUpdateUsageVersion(parames).then(res => {
                        if( res.status === 200) {
                          that.$message({
                            type: 'success',
                            message: '操作成功!'
                          })
                          that.closeDialog(true)
                        }else{
                          that.$message.error(res.info)
                        }
                    });
                } else {
                    return false
                }
            });
        },
    }
}
</script>